home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4519 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: frco.com!usenet
  2. From: Jadam@tcmail.frco.com (Jim Adam)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Missinterpreting *protected* access modifier?
  5. Date: 30 Jan 1996 19:10:06 GMT
  6. Organization: Fisher Rosemount Systems
  7. Message-ID: <4elqee$gqp@rolaids.frco.com>
  8. References: <DLrCGF.G1F@wn.planet.gen.nz>
  9. NNTP-Posting-Host: primrose.frco.com
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.11
  12.  
  13. In article <DLrCGF.G1F@wn.planet.gen.nz>, hugp@kp.planet.gen.nz says...
  14.  
  15. >Am I missinterpreting the meaning of the *protected* access modifier?
  16. >The following code does not compile under VC++:
  17.  
  18. >     ============================================
  19. >     class A
  20. >     {
  21. >     protected:
  22. >       int i;
  23. >       int GetInt() { return i; };
  24. >     };
  25. >
  26. >     class B : public A
  27. >     {
  28. >     protected:
  29. >       int Foo(A * pA) { return pA->GetInt(); } // ERROR
  30. >     };
  31. >     ============================================
  32.  
  33. Yes, you get a compiler error when you call "pA->GetInt()."  The
  34. "protected" access means that you can legally do:
  35.  
  36.    int B::Bar()
  37.    {
  38.       GetInt();  // CALL THE INHERITED GetInt()
  39.    }
  40.  
  41. You can also do:
  42.  
  43.    int B::Foo( B & b )  
  44.    {
  45.      b.GetInt();  // ACCESS PROTECTED STUFF OF ANOTHER INSTANCE
  46.    }
  47.  
  48. But you can't do:
  49.  
  50.    class C : public A { ... };
  51.  
  52.    int B::Foo( C & c )
  53.    {
  54.      C.GetInt();  //  ERROR: SIBLING ACCESS NOT ALLOWED
  55.    }
  56.  
  57. Even though C and B both inherit from A, B can't access
  58. protected members in C.  It can't even access the "common"
  59. protected members inherited from A, such as GetInt().  
  60.  
  61. Jim
  62.  
  63.